/*
 * Author - Erez Raviv <erezraviv@gmail.com>
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */
#ifndef eeprom_h
#define eeprom_h

#include <inttypes.h>


//eeprom data
#define MAX_MODELS  16
#define MAX_MIXERS  32
#define MAX_CURVE5  8
#define MAX_CURVE9  8
#define MAX_KPID    6
#define MAX_CMD     20
#define MAX_CHN     20
#define TOTAL_SOURCES  (MAX_KPID + MAX_CMD + MAX_CHN)

#define KPID_DEVISOR 20

#define SRC_TYPE_CONST    0  //if this is the case then weight holds the value
#define SRC_TYPE_CMD      1
#define SRC_TYPE_PID      2
#define SRC_TYPE_CHN      3
#define SRC_TYPE_GYRO     4
#define SRC_TYPE_ACCEL    5
#define SRC_TYPE_MAG      6
#define SRC_TYPE_BARO     7
#define SRC_TYPE_ATTITUDE 8
#define SRC_TYPE_AP       9
#define SRC_TYPE_COUNT    9

#define NUM_CHNOUT  20
#define NUM_XCHNOUT (NUM_CHNOUT + MAX_KPID)

#define SWITCHES_STR "SW1""SW2""SW3""SW4""SW5""SW6""SW7""SW8""SW9""SWA""SWB""SWC"
#define NUM_SWITCHES 12


#define CURV_STR     "---x>0x<0|x|f>0f<0|f|c1 c2 c3 c4 c5 c6 c7 c8 c9 c10c11c12c13c14c15c16"
#define CURVE_BASE 7
#define CSWITCH_STR  "----   v>ofs  v<ofs  |v|>ofs|v|<ofsAND    OR     XOR    ""v1==v2 ""v1!=v2 ""v1>v2  ""v1<v2  ""v1>=v2 ""v1<=v2 "
#define CSW_NUM_FUNC 14
#define CSW_LEN_FUNC 7

#define CS_OFF       0
#define CS_VPOS      1  //v>offset
#define CS_VNEG      2  //v<offset
#define CS_APOS      3  //|v|>offset
#define CS_ANEG      4  //|v|<offset
#define CS_AND       5
#define CS_OR        6
#define CS_XOR       7
#define CS_EQUAL     8
#define CS_NEQUAL    9
#define CS_GREATER   10
#define CS_LESS      11
#define CS_EGREATER  12
#define CS_ELESS     13
#define CS_MAXF      13  //max function

#define CS_VOFS       0
#define CS_VBOOL      1
#define CS_VCOMP      2
#define CS_STATE(x)   ((x)<CS_AND ? CS_VOFS : ((x)<CS_EQUAL ? CS_VBOOL : CS_VCOMP))

#define LIMITS_MAX 50  //mS
#define LIMITS_MIN 0

#define MLTPX_ADD  0
#define MLTPX_MUL  1
#define MLTPX_REP  2

#define MDVERS      1

typedef struct t_MixData {
    uint8_t  destCh;            //        1..NUM_CHNOUT
    uint8_t  srcType;           //
    uint8_t  srcNum;            //
    int16_t  weight;
    int16_t  sOffset;
    int8_t   swtch;
    uint8_t  curve;             //0=symmetrisch 1=no neg 2=no pos
    uint8_t  delayUp;
    uint8_t  delayDown;
    uint8_t  speedUp;         // Servogeschwindigkeit aus Tabelle (10ms Cycle)
    uint8_t  speedDown;       // 0 nichts
    uint8_t  mltpx;           // multiplex method 0=+ 1=* 2=replace
} __attribute__((packed)) MixData;

typedef struct t_CSwData { // Custom Switches data
    int8_t  v1; //input
    int8_t  v2; //offset
    uint8_t func;
} __attribute__((packed)) CSwData;

typedef struct t_SafetySwData { // Custom Switches data
    int8_t  swtch;
    uint16_t  val;
} __attribute__((packed)) SafetySwData;

typedef struct t_LimitData {
    uint16_t  min; //min pwm pulse
    uint16_t  max; //max pwm pulse
    uint16_t  period; //max pwm pulse
    bool    revert;
} __attribute__((packed)) LimitData;

typedef struct t_KPIDData {
    uint16_t   kp; // a negative value denotes use of a PWM_in value (-1 => chn 1)
    uint16_t   ki;
    uint16_t   kd;
} __attribute__((packed)) KPIDData;


typedef struct t_EEPROMData {
    uint8_t       mdvers;
    uint8_t       numOutputChannels;  //may change, 0 means default value

    KPIDData      kpid[MAX_KPID];
    MixData       mixData[MAX_MIXERS];
    LimitData     limitData[NUM_CHNOUT];
    int8_t        curves5[MAX_CURVE5][5];
    int8_t        curves9[MAX_CURVE9][9];
    CSwData       customSw[NUM_SWITCHES];
    SafetySwData  safetySw[NUM_CHNOUT];
} __attribute__((packed)) EEPROMData;














#endif
/*eof*/

